home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgramD2.iso / Visual Database / Visual BASIC 5.0 (Ent. Edition) / Vb5ent Extractor.EXE / VB / SAMPLES / PGUIDE / PROGWOB / PWOIMPLE.FRM (.txt) < prev    next >
Encoding:
Visual Basic Form  |  1996-11-26  |  10.0 KB  |  302 lines

  1. VERSION 5.00
  2. Begin VB.Form frmImplements 
  3.    BorderStyle     =   1  'Fixed Single
  4.    Caption         =   "Polymorphism and the Implements keyword"
  5.    ClientHeight    =   3510
  6.    ClientLeft      =   45
  7.    ClientTop       =   330
  8.    ClientWidth     =   5310
  9.    LinkTopic       =   "Form1"
  10.    MaxButton       =   0   'False
  11.    MinButton       =   0   'False
  12.    PaletteMode     =   2  'Custom
  13.    Picture         =   "PWOImple.frx":0000
  14.    ScaleHeight     =   3510
  15.    ScaleWidth      =   5310
  16.    StartUpPosition =   3  'Windows Default
  17.    Begin VB.Timer tmrDisplay 
  18.       Interval        =   220
  19.       Left            =   1800
  20.       Top             =   2760
  21.    End
  22.    Begin VB.PictureBox picShapes 
  23.       BackColor       =   &H00FFFFFF&
  24.       Height          =   1095
  25.       Left            =   120
  26.       Picture         =   "PWOImple.frx":0446
  27.       ScaleHeight     =   1035
  28.       ScaleWidth      =   1155
  29.       TabIndex        =   2
  30.       Top             =   2280
  31.       Width           =   1215
  32.    End
  33.    Begin VB.CommandButton cmdLate 
  34.       Caption         =   "&Late Bound"
  35.       Height          =   375
  36.       Left            =   3600
  37.       TabIndex        =   1
  38.       Top             =   3000
  39.       Width           =   1575
  40.    End
  41.    Begin VB.CommandButton cmdEarly 
  42.       Caption         =   "&Early Bound"
  43.       Height          =   375
  44.       Left            =   3600
  45.       TabIndex        =   0
  46.       Top             =   2520
  47.       Width           =   1575
  48.    End
  49.    Begin VB.Label lblLateResult 
  50.       Height          =   255
  51.       Left            =   1560
  52.       TabIndex        =   6
  53.       Top             =   3120
  54.       Width           =   1935
  55.    End
  56.    Begin VB.Label lblEarlyResult 
  57.       Height          =   255
  58.       Left            =   1560
  59.       TabIndex        =   5
  60.       Top             =   2640
  61.       Width           =   1935
  62.    End
  63.    Begin VB.Label Label2 
  64.       Caption         =   "Method Call Overhead"
  65.       BeginProperty Font 
  66.          Name            =   "MS Sans Serif"
  67.          Size            =   9.75
  68.          Charset         =   0
  69.          Weight          =   700
  70.          Underline       =   0   'False
  71.          Italic          =   0   'False
  72.          Strikethrough   =   0   'False
  73.       EndProperty
  74.       Height          =   375
  75.       Left            =   1440
  76.       TabIndex        =   4
  77.       Top             =   2160
  78.       Width           =   3855
  79.    End
  80.    Begin VB.Label Label1 
  81.       Caption         =   $"PWOImple.frx":088C
  82.       Height          =   2055
  83.       Left            =   120
  84.       TabIndex        =   3
  85.       Top             =   120
  86.       Width           =   5055
  87.    End
  88. Attribute VB_Name = "frmImplements"
  89. Attribute VB_GlobalNameSpace = False
  90. Attribute VB_Creatable = False
  91. Attribute VB_PredeclaredId = True
  92. Attribute VB_Exposed = False
  93. Option Explicit
  94. Const NUMOBJECTS = 100
  95. Const NUMREPEATSEARLY As Long = 10000
  96. Const NUMREPEATSLATE As Long = 500
  97. ' For demo purposes, three arrays of object
  98. '   references are kept.  Each object the
  99. '   demo creates has an entry in all three
  100. '   arrays:
  101. ' Array of IShape interfaces;
  102. Private maishEarly(1 To NUMOBJECTS) As IShape
  103. ' Array of Polygon interfaces;
  104. Private mapyg(1 To NUMOBJECTS) As Polygon
  105. ' Array of default interfaces (Polygon,
  106. '   Triangle, or Rectangle, depending on
  107. '   the object).
  108. Private maobjLate(1 To NUMOBJECTS) As Object
  109. ' Time test for early binding calls each
  110. '   of NUMOBJECTS objects early bound,
  111. '   using the IShape interface.  (The
  112. '   TimeTest method is all overhead --
  113. '   it takes no arguments, and immediately
  114. '   returns.)  This is repeated
  115. '   NUMREPEATSEARLY times.
  116. Private Sub cmdEarly_Click()
  117.     Dim lngCt As Long
  118.     Dim intCt As Integer
  119.     Dim timeMark As Long
  120.     ' Disable the display of shapes during
  121.     '   the test.
  122.     tmrDisplay.Enabled = False
  123.     cmdEarly.Caption = "Working..."
  124.     timeMark = timeGetTime
  125.     For lngCt = 1 To NUMREPEATSEARLY
  126.         For intCt = 1 To NUMOBJECTS
  127.             ' Make the calls to TimeTest
  128.             '   through the IShape interface,
  129.             '   which all three classes
  130.             '   (Polygon, Rectangle, and
  131.             '   Triangle) implement.
  132.             maishEarly(intCt).TimeTest
  133.         Next
  134.     Next
  135.     timeMark = timeGetTime - timeMark
  136.     lblEarlyResult = ShowElapsed(timeMark, _
  137.         NUMOBJECTS * NUMREPEATSEARLY, _
  138.         "Early Bound Call Overhead")
  139.     cmdEarly.Caption = "&Early Bound"
  140.     '
  141.     ' Start displaying shapes again.
  142.     tmrDisplay.Enabled = True
  143. End Sub
  144. Private Sub cmdLate_Click()
  145.     Dim lngCt As Long
  146.     Dim intCt As Integer
  147.     Dim timeMark As Long
  148.     ' Disable the display of shapes during
  149.     '   the test.
  150.     tmrDisplay.Enabled = False
  151.     cmdLate.Caption = "Working..."
  152.     timeMark = timeGetTime
  153.     For lngCt = 1 To NUMREPEATSLATE
  154.         For intCt = 1 To NUMOBJECTS
  155.             ' Make the calls to TimeTest
  156.             '   late-bound, through the default
  157.             '   interfaces of the objects
  158.             '   (Polygon, Rectangle, and
  159.             '   Triangle).
  160.             maobjLate(intCt).TimeTest
  161.         Next
  162.     Next
  163.     timeMark = timeGetTime - timeMark
  164.     lblLateResult = ShowElapsed(timeMark, _
  165.         NUMOBJECTS * NUMREPEATSLATE, _
  166.         "Late Bound Call Overhead")
  167.     cmdLate.Caption = "&Late Bound"
  168.     '
  169.     ' Start displaying shapes again.
  170.     tmrDisplay.Enabled = True
  171. End Sub
  172. Private Sub Form_Unload(Cancel As Integer)
  173.     ' Free resources associated with the
  174.     '   form, by clearing the hidden
  175.     '   global variable.
  176.     Set frmImplements = Nothing
  177. End Sub
  178. ' If the shape-display picture box is
  179. '   clicked, display all the shapes at
  180. '   once (using early binding).
  181. Private Sub picShapes_Click()
  182.     Dim intCt As Integer
  183.     For intCt = 1 To NUMOBJECTS
  184.         Call maishEarly(intCt).DrawToPictureBox(picShapes)
  185.     Next
  186. End Sub
  187. Private Sub Form_Load()
  188.     Dim intCt As Integer
  189.     Dim asngPoints() As Single
  190.     Set Picture = New StdPicture
  191.     Call Randomize(Timer)
  192.     For intCt = 1 To NUMOBJECTS
  193.         ' Randomly create 1/3 Polygons,
  194.         '   1/3 Triangles, and 1/3 Rectangles.
  195.         '   Store the reference to each of
  196.         '   these objects in the late-bound
  197.         '   array.
  198.         Select Case Int(Rnd * 3)
  199.             Case 0
  200.                 Set maobjLate(intCt) = New Polygon
  201.             Case 1
  202.                 Set maobjLate(intCt) = New Triangle
  203.             Case 2
  204.                 Set maobjLate(intCt) = New Rectangle
  205.         End Select
  206.         ' Save a reference to the object's
  207.         '   IShape interface, to demonstrate
  208.         '   early binding using polymorphism.
  209.         '   Each of the three classes
  210.         '   implements IShape, so Visual
  211.         '   Basic is able to query for the
  212.         '   IShape interface and make the
  213.         '   assignment.
  214.         Set maishEarly(intCt) = maobjLate(intCt)
  215.         ' Save a reference to the object's
  216.         '   Polygon interface, as well.
  217.         Set mapyg(intCt) = maobjLate(intCt)
  218.         ' If the object was a Polygon (rather
  219.         '   than a Triangle or Rectangle,
  220.         '   which simply implement the
  221.         '   Polygon interface), it will have
  222.         '   only one point.  Give it a
  223.         '   random number of points (from
  224.         '   four to 24).
  225.         If mapyg(intCt).GetPointCount = 1 Then
  226.             ReDim asngPoints(0 To (Int(21 * Rnd) + 4) * 2 - 1)
  227.             Call mapyg(intCt).SetPoints(asngPoints)
  228.         End If
  229.         ' Assign the object a random color.
  230.         mapyg(intCt).Color = Int(Rnd * &HFFFFFF)
  231.     Next
  232.     Debug.Print "If you go back and look at the debug numbers"
  233.     Debug.Print "of the shape objects, you'll notice that "
  234.     Debug.Print "a lot more than " & NUMOBJECTS & " objects were created."
  235.     Debug.Print "This is because each Triangle and each"
  236.     Debug.Print "Rectangle creates an inner Polygon object."
  237.     '
  238.     ' Assign random values to each
  239.     '   point in each object.
  240.     Call NewShapes
  241. End Sub
  242. Private Sub tmrDisplay_Timer()
  243.     ' Iterate repeatedly through the
  244.     '   shape objects, displaying
  245.     '   them in a PictureBox.
  246.     Static intCt As Integer
  247.     picShapes.Cls
  248.     intCt = intCt + 1
  249.     If intCt > NUMOBJECTS Then intCt = 1
  250.     Call maishEarly(intCt).DrawToPictureBox(picShapes)
  251. End Sub
  252. ' ShowElapsed helper procedure displays
  253. ' -----------       the result of an early
  254. '   or late-bound time test.
  255. Private Function ShowElapsed(ByVal Milliseconds As Long, _
  256.         ByVal Iterations As Long, _
  257.         ByVal Caption As String) As String
  258.     Dim strMSec As String
  259.     strMSec = Format$(Milliseconds / Iterations, "0.0000")
  260.     MsgBox Format$(Iterations, "#,###,##0") _
  261.         & " iterations in " _
  262.         & Format$(Milliseconds / 1000#, "##,##0.00") _
  263.         & " seconds" & vbCrLf _
  264.         & strMSec _
  265.         & " milliseconds per call", , Caption
  266.         
  267.     ShowElapsed = strMSec & " msec/call"
  268. End Function
  269. ' NewShapes changes the shape of each of
  270. ' ---------     the objects, calling
  271. '   MakeRandomPoints to generate a set
  272. '   of random points.  It does not change
  273. '   the number of points in a Polygon.
  274. Private Sub NewShapes()
  275.     Dim intCt As Integer
  276.     Dim intPt As Integer
  277.     Dim pyg As Polygon
  278.     Dim asngPoints() As Single
  279.     Dim intNumPts As Integer
  280.     For intCt = 1 To NUMOBJECTS
  281.         intNumPts = mapyg(intCt).GetPointCount
  282.         Call MakeRandomPoints(intNumPts, asngPoints)
  283.         Call mapyg(intCt).SetPoints(asngPoints)
  284.     Next
  285. End Sub
  286. ' MakeRandomPoints creates a set of random
  287. ' ----------------      points for a
  288. '   Polygon object, placing them in the
  289. '   zero-based, one-dimensional array the
  290. '   SetPoints method requires.
  291. Private Sub MakeRandomPoints( _
  292.         ByVal intNumPts As Integer, _
  293.         asngPoints() As Single)
  294.         
  295.     Dim intPt As Integer
  296.     ReDim asngPoints(0 To intNumPts * 2 - 1)
  297.     For intPt = 0 To intNumPts * 2 - 1 Step 2
  298.         asngPoints(intPt) = Rnd * picShapes.ScaleWidth
  299.         asngPoints(intPt + 1) = Rnd * picShapes.ScaleHeight
  300.     Next
  301. End Sub
  302.